package com.first.wchat.connect.udp;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import com.first.wchat.data.Constant;

import android.content.Context;
import android.util.Log;

public class UDPSender {
	
	DataSendListener mListener;

	public UDPSender()
	{

	}

	public void setListener(DataSendListener l)
	{
		mListener = l;
	}
	
	public void send(final Context ctx ,final String msg) { 
		
		new Thread(new Runnable()
		{

			@Override
			public void run() {
					String message= msg;
				 	message = (message == null ? "REJIN" : message);  
			        int server_port = 12345;  
			        DatagramSocket s = null;  
			        try {  
			            s = new DatagramSocket();  
			        } catch (SocketException e) {  
			            e.printStackTrace();  
			        }  
			        InetAddress local = null;  
			        try {  
			            local = InetAddress.getByName("localhost");  
			        } catch (UnknownHostException e) {  
			            e.printStackTrace();  
			        }  
			        int msg_length = message.length();  
			        byte[] messageByte = message.getBytes();  
			        DatagramPacket p = new DatagramPacket(messageByte, msg_length, local,  
			                server_port);  
			        
			
			        
			        String data = "UDP sender================ to:"+local 
                    + " Data:" + message;
			        if(mListener != null)
			        	mListener.send(data);
			        Log.d("UDP sender================", local 
                            + ":" + message); 
			        try {  
			            s.send(p);  
			        } catch (IOException e) {  
			            e.printStackTrace();  
			            s.close();
			        }  
			        
			        s.close();
			   
				
			}
			
		}).start();
	}
	
	public void send(final Context ctx ,final String msg,final String name) { 
		
		new Thread(new Runnable()
		{

			@Override
			public void run() {
					String message= msg;
				 	message = (message == null ? "REJIN" : message);  
			        int server_port = Constant.PORT;  
			        DatagramSocket s = null;  
			        try {  
			            s = new DatagramSocket(); 
			            //s.setBroadcast(false);
			        } catch (SocketException e) {  
			            e.printStackTrace();  
			        }  
			        InetAddress local = null;  
			        try {  
			            local = InetAddress.getByName(name);  
			        } catch (UnknownHostException e) {  
			            e.printStackTrace();  
			        }  
			        
			      String data = null;
				try {
					data = new String(message.getBytes("UTF-8"));
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
			        int msg_length = data.length();  
			        byte[] messageByte = data.getBytes();  
			        DatagramPacket p = new DatagramPacket(messageByte, msg_length, local,  
			                server_port);  
			        
			
			        
			        String pack = "UDP sender================ to:"+local 
                    + " Data:" + message;
			        if(mListener != null)
			        	mListener.send(pack);
			        Log.d("UDP sender================", local 
                            + ":" + message); 
			        
			        
			        try {  
			            s.send(p);  
			        } catch (IOException e) {  
			            e.printStackTrace();  
			        }  
			   
				
			}
			
		}).start();
	}
       
}
